using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    public class ClickedLabel : VisualElement {

        Label label;
        public event Action<EventBase> onDoubleClick;

        private GenericMenu menu;
        public bool enableMenu;
        public string text {
            get => label.text;
            set => label.text = value;
        }

        public ClickedLabel(string text) {
            label = new Label(text);
            label.RegisterCallback<MouseDownEvent>(ClickCallback);
            Add(label);
        }

        private void ClickCallback(MouseDownEvent evt) {
            if (evt.clickCount == 2 && evt.button == 0) {
                // 双击
                onDoubleClick?.Invoke(evt);
            }
            if (enableMenu && menu != null && evt.clickCount == 1 && evt.button == 1) {
                // 右击
                var menuPos = this.LocalToWorld(new Vector2(0, worldBound.height + 5));
                menu.DropDown(new Rect(menuPos, Vector2.zero));
            }
        }

        public void AddMenuItem(string path, GenericMenu.MenuFunction action) {
            if (menu == null) {
                menu = new GenericMenu();
            }
            menu.AddItem(new GUIContent(path), false, action);
        }
        public void AddMenuSeparator() {
            if (menu == null) {
                menu = new GenericMenu();
            }
            menu.AddSeparator(null);
        }


    }
}
